/*
* BaseAction.java
*
* 版本信息	v1.0
*
* 日期		2006-9-5 10:07:00
*
* 修改历史	
* 
* 作者		Ammen
*
*/
package com.sinovatech.ologger.core;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.StringConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataIntegrityViolationException;

import com.sinovatech.core.web.support.SqlDateConverter;
import com.sinovatech.core.web.support.TimestampConverter;
import com.sinovatech.ologger.actionlog.aop.annotation.ActionMethodInvocation;
import com.sinovatech.ologger.actionlog.core.LogHelper;
import com.sinovatech.ologger.actionlog.core.LogUser;
import com.sinovatech.ologger.util.LogUtil;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.webwork.interceptor.SessionAware;
import com.opensymphony.xwork.ActionSupport;

@SuppressWarnings( { "serial", "unchecked" })
public abstract class BaseAction extends ActionSupport implements SessionAware {

	public static final String STAFF_CONDITION_VO = "staff_condition_vo";
	public static final String GET_METHOD_ENCODING = "ISO-8859-1";
	public static final String SERVER_ENCODING = "UTF-8";
	protected final Log logger = LogFactory.getLog(getClass());
	protected Map session;
	private Exception exception = null;
	protected String statereminErrorMessage;
	protected MethodInterceptor methodInterceptor;

	public abstract String process();

	public void setSession(Map session) {
		this.session = session;
	}

	/**
	 * 取得HttpSession的简化方法.
	 */
	protected HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}

	/**
	 * 取得HttpRequest的简化方法.
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 取得HttpResponse的简化方法.
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public static void registConverterDefaultNull() {
		ConvertUtils.register(new StringConverter(), String.class);
		ConvertUtils.register(new IntegerConverter(null), Integer.class);
		ConvertUtils.register(new LongConverter(null), Long.class);
		ConvertUtils.register(new FloatConverter(null), Float.class);
		ConvertUtils.register(new DoubleConverter(null), Double.class);
		//ConvertUtils.register(new DateConverter("yyyy-MM-dd"), Date.class);
		ConvertUtils.register(new SqlDateConverter("yyyy-MM-dd"), java.sql.Date.class);
		ConvertUtils.register(new TimestampConverter("yyyy-MM-dd"), java.sql.Timestamp.class);
	}

	static {
		registConverterDefaultNull();
	}

	@Override
	public final String execute() throws Exception {

		String forward = null;
		try {
			try {
				MethodInvocation invocation = new ActionMethodInvocation(this);
				LogHelper.setUser((LogUser) session.get("loginUser"));
				forward = (String) methodInterceptor.invoke(invocation);
			} catch (Throwable e) {
				logger.error(e.getMessage());
				forward = process();
			}
			return forward;
		} catch (RuntimeException e) {
			exception = e;
			logger.error(LogUtil.stackTraceToString(e));
			if (e instanceof DataIntegrityViolationException)
				return "/WEB-INF/jsp/error/error.jsp";
			return "/WEB-INF/jsp/error/runtimeerrorpage.jsp";
		}

	}

	public Exception getException() {
		return exception;
	}

	public void setException(Exception exception) {
		this.exception = exception;
	}

	public String getStatereminErrorMessage() {
		return statereminErrorMessage;
	}

	public void setStatereminErrorMessage(String statereminErrorMessage) {
		this.statereminErrorMessage = statereminErrorMessage;
	}

	public void setOperationLogInterceptor(MethodInterceptor methodInterceptor) {
		this.methodInterceptor = methodInterceptor;
	}

}
